package generic.gui.util;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JFormattedTextField;
import javax.swing.SwingConstants;
import javax.swing.text.NumberFormatter;

public class FocusEstadoTextFormatter implements FocusListener {
	
	private Object value;
	private NumberFormatter numberFormatter;
	private JFormattedTextField formattedTextField;
	
	public FocusEstadoTextFormatter(JFormattedTextField formattedTextField) {
		setFormattedTextField(formattedTextField);
	}
	public void focusGained(FocusEvent arg0) {
		setValue(getFormattedTextField().getValue());
		getFormattedTextField().setValue(null);	
		getFormattedTextField().setHorizontalAlignment(SwingConstants.CENTER);
	}
	public void focusLost(FocusEvent e) {
		if (getFormattedTextField().getText().length() == 0)
			getFormattedTextField().setValue(getValue());
		
		getFormattedTextField().setHorizontalAlignment(SwingConstants.RIGHT);
	}
	public Object getValue() {
		return value;
	}
	public void setValue(Object value) {
		this.value = value;
	}
	public NumberFormatter getNumberFormatter() {
		return numberFormatter;
	}
	public void setNumberFormatter(NumberFormatter numberFormatter) {
		this.numberFormatter = numberFormatter;
	}
	public JFormattedTextField getFormattedTextField() {
		return formattedTextField;
	}
	public void setFormattedTextField(JFormattedTextField formattedTextField) {
		this.formattedTextField = formattedTextField;
	}
}
